Pandas中的时间日期转换 您所在的位置:网站首页 excel 时间转换为时间戳 Pandas中的时间日期转换

Pandas中的时间日期转换

2024-05-25 04:44| 来源: 网络整理| 查看: 265

小伙伴们大家好,相信大家在使用pandas的过程中一定避免不了类型转换的问题,尤其是时间类型的转换,这里我尽可能用最短的篇幅帮大家速通pandas中的时间类型转换问题。

将数字转换成datetime

将时间戳的数字转换为datetime类型

df = pd.DataFrame({'date': [1470195805, 1480195805, 1674725925]})image.png1.使用to_datetime函数

我们调用to_datetime()函数,并传入参数 unit='s'

image.png2.使用astype函数df['date'].astype('datetime64[s]')image.png 这里datetime64位NumPy类型,常见单位如下: CodeMeaningYyearMmonthDdayhhourmminutessecondmsmillisecondnsnanosecond将字符串转换为datetime

在pandas中,string以object的形式出现。无论使用to_datetime还是astype函数都可以完成字符串到时间日期的转换。

df = pd.DataFrame({'date':['3/10/2019','3/11/2020','3/12/2021']}) image.png1.使用to_datetime函数pd.to_datetime(df['date'])


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有